import 'package:flutter/material.dart';
import 'package:get/get.dart';
import '../../../../models/message.dart';
import '../../../../services/httpsClient.dart';
import '../../../../services/storage.dart';
import '../../../user/controllers/user_controller.dart';

class PassLoginController extends GetxController {
  //TODO: Implement PassLoginController
  UserController userController = Get.find<UserController>();

  HttpsClient httpsClient = HttpsClient();

  TextEditingController telController = TextEditingController();
  TextEditingController passController = TextEditingController();
  @override
  void onInit() {
    super.onInit();
  }

  @override
  void onClose() {
    super.onClose();
    userController.getUserInfo();
  }

  Future<MessageModel> doLogin() async {
    var res = await httpsClient.post("api/doLogin", data: {
      "username": telController.text,
      "password": passController.text
    });
    if (res != null) {
      if (res.data['success']) {
        print(res.data);
        // 注册成功执行登录，信息保存到本地
        Storage.setData("userinfo", res.data['userinfo']);
        return MessageModel(message: "登录成功", success: true);
      } else {
        return MessageModel(message: res.data['message'], success: false);
      }
    } else {
      return MessageModel(message: "网络异常", success: false);
    }
  }
}
